<script>
	import cache from '@/config/cache'
	import utils from '@/config/utils'
	import provinceList from '@/json/area_province.js';
	import cityList from '@/json/area_city.js';
	import districtList from '@/json/area_district.js';

	export default {
		globalData: {
			provinceList: provinceList,
			cityList: cityList,
			districtList: districtList,
		},
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// set token
			setToken(token) {
				cache.set("token", token);
			},

			// 判断空
			isEmpty(str) {
				return utils.isEmpty(str);
			},
			
			// 获取token
			getToken() {
				var token = cache.get("token");
				if (utils.isEmpty(token)) {
					return "";
				}
				return token;
			},


			// 获取用户id
			getUserId() {
				return this.getUser().id || null;
			},

			// 替换用户
			setUser(user) {
				user.token = user.token || this.getToken();
				cache.set("userinfo", user);
			},

			// 获取登录用户信息
			getUser() {
				var user = cache.get("userinfo");
				if (utils.isEmpty(user)) {
					return null;
				}
				return user;
			},
			
			
			// 是否登录
			isLogin(){
				var token = this.getToken()	;
				var userId = this.getUser().id	;
				return !this.isEmpty(token) && !this.isEmpty(userId);
			},
			

			// 移除用户信息
			removeUser() {
				cache.remove("userinfo");
				cache.remove("token");
				cache.remove("userid");
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
		@import "@/uni_modules/uview-ui/index.scss";
</style>
